<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.goodsId ? '新增运营商' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="运营商区域" prop="">
      <el-select v-model="dataForm.provinceId" placeholder="请选择省" @change="provinceChanged">
        <el-option 
          v-for="item in dataForm.provinceList"
          :key="item.id"
          :label="item.areaName"
          :value="item.id">
        </el-option>
      </el-select>
      <el-select v-model="dataForm.cityId" placeholder="请选择市" @change="provinceChanged1" v-show="this.dataForm.provinceId !== ''">
        <el-option 
          v-for="item in dataForm.cityList"
          :key="item.id"
          :label="item.areaName"
          :value="item.id">
        </el-option>
      </el-select>
      <el-select v-model="dataForm.areaId" placeholder="请选择区" v-show="this.dataForm.cityId !== ''">
        <el-option 
          v-for="item in dataForm.areaList"
          :key="item.id"
          :label="item.areaName"
          :value="item.id">
        </el-option>
      </el-select>
      <el-button type="primary" round @click="addAddress()" v-show="this.dataForm.provinceId !== ''">添加</el-button>
    </el-form-item>
    <el-form-item label="会员账号" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="会员账号" style="width:200px" disabled= true></el-input>
    </el-form-item>
    <el-form-item label="运营商名称" prop="trueName">
      <el-input v-model="dataForm.trueName" placeholder="运营商名称" style="width:200px"></el-input>
    </el-form-item>
    <el-form-item label="运营商手机号" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="运营商手机号" style="width:200px"></el-input>
    </el-form-item>
    <el-form-item label="运营商责人" prop="realName">
      <el-input v-model="dataForm.realName" placeholder="运营商责人" style="width:200px"></el-input>
    </el-form-item>
    <!-- <el-form-item label="运营商等级" prop="userType">
      <el-radio-group v-model="dataForm.userType">
        <el-radio :label="0">省</el-radio>
        <el-radio :label="1">市</el-radio>
        <el-radio :label="2">级</el-radio>
      </el-radio-group>
    </el-form-item> -->
    <!-- <el-form-item label="上级账号" prop="panUserName">
      <el-input v-model="dataForm.panUserName" placeholder="上级账号" style="width:200px"></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="现金券余额" prop="integral">
      <el-input v-model="dataForm.integral" placeholder="现金券余额" style="width:100px" disabled></el-input>
    </el-form-item>
    <el-form-item label="总金额/元" prop="balanceMoney">
      <el-input v-model="dataForm.balanceMoney" placeholder="总金额/元" style="width:100px" disabled></el-input>
    </el-form-item>
    <el-form-item label="未提现/元" prop="notExtract">
      <el-input v-model="dataForm.notExtract" placeholder="未提现/元" style="width:100px" disabled></el-input>
    </el-form-item>
    <el-form-item label="已提现/元" prop="yesExtract">
      <el-input v-model="dataForm.yesExtract" placeholder="已提现/元" style="width:100px" disabled></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        isDisabled: false,
        visible: false,
        dataForm: {
          userName: '',
          trueName: '',
          realName: '',
        //   userType: '',
        //   panUserName: '',
          integral: 0,
          balanceMoney: 0,
          notExtract: 0,
          yesExtract: 0,
          mobile: 0,
          provinceId: '',
          provinceList: [],
          cityId: '',
          cityList: [],
          areaId: '',
          areaList: [],
        },
        dataRule: {
          userName: [
            { required: true, message: '会员账号不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '运营商手机号不能为空', trigger: 'blur' }
          ],
          trueName: [
            { required: true, message: '运营商名称不能为空', trigger: 'blur' }
          ],
          realName: [
            { required: true, message: '运营商责人不能为空', trigger: 'blur' }
          ]
          // integral: [
          //   { required: true, message: '现金券余额不能为空', trigger: 'blur' },
          //   { pattern: /^[0-9]+$/, message: '现金券余额为正整数', trigger: 'blur' }
          // ],
          // balanceMoney: [
          //   { required: /^[0-9]+$/, message: '总金额/元不能为空', trigger: 'blur' },
          //   { pattern: /^[0-9]+[.]{1}[0-9]{2}$/, message: '总金额/元为两位小数', trigger: 'blur' }
          // ],
          // notExtract: [
          //   { required: true, message: '未提现/元不能为空', trigger: 'blur' },
          //   { pattern: /^[0-9]+[.]{1}[0-9]{2}$/, message: '未提现/元为两位小数', trigger: 'blur' }
          // ],
          // yesExtract: [
          //   { required: true, message: '已提现/元不能为空', trigger: 'blur' },
          //   { pattern: /^[0-9]+[.]{1}[0-9]{2}$/, message: '已提现/元为两位小数', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init () {
        this.visible = true
        this.isDisabled = false
        this.dataForm.userName = ''
        this.dataForm.trueName = ''
        this.dataForm.realName = ''
        this.dataForm.integral = ''
        this.dataForm.balanceMoney = ''
        this.dataForm.notExtract = ''
        this.dataForm.yesExtract = ''
        this.dataForm.mobile = ''
        this.$http({
          url: this.$http.adornUrl('/member/area/queryProvinceList'),
          method: 'post'
        }).then(({data}) => {
          this.dataForm.provinceList = data.data
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isDisabled = true
            this.$http({
              url: this.$http.adornUrl('/member/user/addOperator'),
              method: 'post',
              data: this.$http.adornData({
                'userName': this.dataForm.userName,
                'trueName': this.dataForm.trueName,
                'realName': this.dataForm.realName,
                'integral': 0,
                'balanceMoney': 0,
                'notExtract': 0,
                'yesExtract': 0,
                'mobile': this.dataForm.mobile
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      provinceChanged (id) {
        this.$http({
          url: this.$http.adornUrl('/member/area/querySubordinateList'),
          method: 'post',
          data: this.$http.adornData(id, false)
        }).then(({data}) => {
          this.dataForm.cityId = ''
          this.dataForm.areaId = ''
          this.dataForm.cityList = data.data
        })
      },
      provinceChanged1 (id) {
        this.$http({
          url: this.$http.adornUrl('/member/area/querySubordinateList'),
          method: 'post',
          data: this.$http.adornData(id, false)
        }).then(({data}) => {
          this.dataForm.areaId = ''
          this.dataForm.areaList = data.data
        })
      },
      addAddress() {
        if (this.dataForm.provinceId !== '') {
          if (this.dataForm.cityId !== '') {
            if (this.dataForm.areaId !== '') {
              this.dataForm.userName = this.dataForm.areaId
            } else {
              this.dataForm.userName = this.dataForm.cityId
            }
          } else {
            this.dataForm.userName = this.dataForm.provinceId
          }
        }
        this.$http({
          url: this.$http.adornUrl(`/member/user/getIsExistenceByUserName/${this.dataForm.userName}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            if (data.data >= 0) {
              this.$message.error('该区域的运营商已存在！')
              this.dataForm.userName = ''
            }
          }
        })
      }
    }
  }
</script>
